/* Kanvas Paint and Drawing Tool.
 * Copyright (C) 2011 Darryl Wallace
 * wallacdj@gmail.com / darryl@wallynet.ca
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/*!
  @file EraserTool.cpp
  @author Darryl Wallace
  @date 2011
  @brief This file defines the eraser tool.
  */

#include "EraserTool.h"
#include "LayerItem.h"


EraserTool::EraserTool()
    : _parent(NULL), _eraserSize(11)
{
    // initialize eraser
    _eraserImage = QImage(_eraserSize,_eraserSize,QImage::Format_ARGB32);
    _eraserImage.fill(0x00000000);
    QPainter p(&_eraserImage);
    QPen pen;
    pen.setColor(Qt::black);
    QBrush brush(Qt::black);
    p.setPen(pen);
    p.setBrush(brush);
//    p.setRenderHint(QPainter::Antialiasing);
    p.drawEllipse(0,0,_eraserSize,_eraserSize);

    // invert the image
    for (int i=0; i<_eraserSize;i++)
        for (int j=0;j<_eraserSize;j++)
        {
            int alpha = qAbs(qAlpha(_eraserImage.pixel(i,j)) - 255);
            _eraserImage.setPixel(i,j,qRgba(0,0,0,alpha));
        }
}

EraserTool::~EraserTool()
{
}

void EraserTool::setLayerItem(LayerItem *layer)
{
    setParentItem((QGraphicsItem *)layer );
    _parent=layer;
}
void EraserTool::setPrimaryColor(const QColor & p)
{
    Q_UNUSED(p);
}

void EraserTool::setSecondaryColor(const QColor &s)
{
    Q_UNUSED(s);
}

void EraserTool::paint(QPainter *painter, const QStyleOptionGraphicsItem * /*option*/, QWidget * /*widget*/)
{
    Q_UNUSED(painter);
}

void EraserTool::paintOnImage(QPainter *painter)
{
    Q_UNUSED(painter);
}

void EraserTool::handleMousePressEvent(const QGraphicsSceneMouseEvent *e)
{
    _initial=e->scenePos();
    _last=e->scenePos();
    _current=e->scenePos();

    erase();

}

void EraserTool::handleMouseMoveEvent(const QGraphicsSceneMouseEvent *e)
{
    _last=_current;
    _current=e->scenePos();

    erase();
}

void EraserTool::handleMouseReleaseEvent(const QGraphicsSceneMouseEvent *e)
{
    // Set the last position.  Not currently used.
    _last=e->scenePos();
}

QIcon EraserTool::toolIcon() const
{
    QIcon icon(":/KanvasStandardPlugin/icons/eraser.png");
    return icon;
}

void EraserTool::setPen(const QPen &pen)
{
    Q_UNUSED(pen);
}

void EraserTool::erase()
{
    int x = _current.x()-_eraserSize/2;
    int y = _current.y()-_eraserSize/2;
    int __y;

    QImage *img = _parent->getImagePtr();
    int w = img->size().width();
    int h = img->size().height();

    for (int i=0;i<_eraserSize;i++)
    {
        x++;
        if (x>-1 && x<w)
        {
            for (int j=0;j<_eraserSize;j++)
            {
                __y=y+j;
                if (__y>-1 && __y<h)
                {
                    QRgb c = img->pixel(x, __y);
                    int alpha = qAlpha(c);
                    int eraserAlpha = qAlpha(_eraserImage.pixel(i,j));
                    // need to adjust the alpha value of the pixel existing pixel
                    // by the fraction of the eraser's alpha / 255.
                    if (eraserAlpha>0 && eraserAlpha<255)
                    {
                        eraserAlpha = alpha * eraserAlpha / 255;
                    }
                    if (eraserAlpha!=255)
                        img->setPixel(x,__y,qRgba(qRed(c), qGreen(c), qBlue(c), eraserAlpha));
                }
            }
        }
    }
}

